package com.bootdo.lvyou_specialty.service.impl;

import com.bootdo.common.utils.MD5Utils;
import com.bootdo.system.dao.UserDao;
import com.bootdo.system.dao.UserRoleDao;
import com.bootdo.system.domain.UserDO;
import com.bootdo.system.domain.UserRoleDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bootdo.lvyou_specialty.dao.LvyouSpecialtyMerchantDao;
import com.bootdo.lvyou_specialty.domain.LvyouSpecialtyMerchant;
import com.bootdo.lvyou_specialty.service.LvyouSpecialtyMerchantService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class LvyouSpecialtyMerchantServiceImpl implements LvyouSpecialtyMerchantService {
	@Autowired
	private LvyouSpecialtyMerchantDao lvyouSpecialtyMerchantDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private UserRoleDao userRoleDao;
	
	@Override
	public LvyouSpecialtyMerchant get(Long id){
		return lvyouSpecialtyMerchantDao.get(id);
	}
	
	@Override
	public List<LvyouSpecialtyMerchant> list(Map<String, Object> map){
		return lvyouSpecialtyMerchantDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return lvyouSpecialtyMerchantDao.count(map);
	}
	
	@Override
	public int save(LvyouSpecialtyMerchant lvyouSpecialtyMerchant){
	
		if(lvyouSpecialtyMerchant.getId() != null) {
			return lvyouSpecialtyMerchantDao.update(lvyouSpecialtyMerchant);
		}else {
			return lvyouSpecialtyMerchantDao.save(lvyouSpecialtyMerchant);
		}		
	
		
	}
	
	@Override
	public int update(LvyouSpecialtyMerchant lvyouSpecialtyMerchant){
		return lvyouSpecialtyMerchantDao.update(lvyouSpecialtyMerchant);
	}
	
	@Override
	public int remove(Long id){
		return lvyouSpecialtyMerchantDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return lvyouSpecialtyMerchantDao.batchRemove(ids);
	}

	@Override
	public int updateUserId(Long userId, Long id) {
		return lvyouSpecialtyMerchantDao.updateUserId(userId, id);
	}

	@Transactional
	@Override
	public int updateUser(UserDO userDO, Long merchantId) {
		int i = 0;
		if (userDO.getUserId() == null) {
			userDO.setPassword(MD5Utils.encrypt(userDO.getUsername(), userDO.getPassword()));
			userDO.setMerchantId(merchantId);

			userDao.save(userDO);
			Long userId = userDO.getUserId();
			List<Long> roles = userDO.getRoleIds();
			userRoleDao.removeByUserId(userId);
			List<UserRoleDO> list = new ArrayList<>();
			for (Long roleId : roles) {
				UserRoleDO ur = new UserRoleDO();
				ur.setUserId(userId);
				ur.setRoleId(roleId);
				list.add(ur);
			}
			if (list.size() > 0) {
				userRoleDao.batchSave(list);
			}
			i = lvyouSpecialtyMerchantDao.updateUserId(userDO.getUserId(), merchantId);

		} else {

			i = userDao.update(userDO);
		}
		return i;
	}

	@Transactional
	@Override
	public int deleteUser(UserDO userDO, Long merchantId) {

		int i = 0;

		userDao.remove(userDO.getUserId());
		userRoleDao.removeByUserId(userDO.getUserId());

		i = lvyouSpecialtyMerchantDao.updateUserId(null, merchantId);

		return i;
	}
}
